package com.wangwenjun.concurrency.chapter9;

public class CigaretteTakenTest {

    private static Object lock = new Object();

    static boolean hasCigarette = false;

    static boolean hasTaken = false;

    public static void main(String[] args) throws InterruptedException {


        new Thread(()->{
            synchronized (lock){
                while (!hasCigarette){
                    System.out.println("没有烟,先休息");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                if(hasCigarette){
                    System.out.println("有烟,开始工作");
                }else{
                    System.out.println("没烟,没干成活");
                }
            }
        }).start();


        new Thread(()->{
            synchronized (lock){
                while (!hasTaken){
                    System.out.println("没有外卖,先休息");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                if(hasTaken){
                    System.out.println("有外卖,开始工作");
                }else{
                    System.out.println("没外卖,没干成活");
                }
            }
        }).start();


        Thread.sleep(500);


        new Thread(()->{
            synchronized (lock){
                hasCigarette = true;
                lock.notifyAll();
            }
        }).start();







    }

}
